import requests
from playwright.sync_api import Playwright, sync_playwright

# 检查 PLOS ONE 的投稿状态
# 假设投稿系统的地址为: https://www.editorialmanager.com/pone/default2.aspx
# 假设投稿账号为:1234@email.com
# 假设投稿密码为:123456
def checkPlosOne(playwright: Playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.editorialmanager.com/pone/default2.aspx")
    page.locator("iframe[name=\"content\"]").content_frame.locator("iframe[name=\"login\"]").content_frame.locator(
        "#username").click()
    # 这一段输入用户名
    page.locator("iframe[name=\"content\"]").content_frame.locator("iframe[name=\"login\"]").content_frame.locator(
        "#username").fill("1234@email.com")
    page.locator("iframe[name=\"content\"]").content_frame.locator("iframe[name=\"login\"]").content_frame.locator(
        "#passwordTextbox").click()
    # 这一段输入密码
    page.locator("iframe[name=\"content\"]").content_frame.locator("iframe[name=\"login\"]").content_frame.locator(
        "#passwordTextbox").fill("123456")
    page.locator("iframe[name=\"content\"]").content_frame.locator("iframe[name=\"login\"]").content_frame.get_by_role(
        "button", name="Author Login").click()
    page.locator("iframe[name=\"content\"]").content_frame.get_by_role("link",
                                                                       name="Submissions Being Processed").click()

    # 获取id为row1的元素下面的文本tr元素，然后获取弟5个td元素的文本
    currentStatue = page.locator("iframe[name=\"content\"]").content_frame.locator("#row1").locator("td").nth(
        4).inner_text()

    print('当前状态' + currentStatue)
    # # 发送消息
    sendWXAlert('PlosOne:' + currentStatue, '')

    # ---------------------
    context.close()
    browser.close()

    page.close()


# 发送微信消息
# post方式发送微信消息，地址是扫码关注注册之后息知给提供的
def sendWXAlert(title, message):
    url = 'https://xizhi.qqoq.net/xxx.send'
    data = {
        'title': title,
        'content': message
    }
    response = requests.post(url, data=data)
    print(response.text)

# 入口方法
with sync_playwright() as playwright:
    try:
      checkPlosOne(playwright)
    except Exception as e:
         print('出现异常' + str(e))
